package com.night.card.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.night.card.mapper.CardInMachineMapper;
import com.night.card.service.interfs.CardInMachineService;
import com.night.common.entity.card.CardInMachine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: CardInMachineServiceImp
 * @Description: 充值卡机service层实现
 * @author: CHX42
 * @date: 2022/10/21-9:26
 * @version: v1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CardInMachineServiceImp implements CardInMachineService {
    @Value("${ss.card.pageSize}")
    private int pageSize;

    @Resource
    private CardInMachineMapper cardInMachineMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public void add(CardInMachine cardInMachine) {
        cardInMachineMapper.insert(cardInMachine);
    }

    @Override
    public void update(CardInMachine cardInMachine) {
        cardInMachineMapper.update(cardInMachine);
    }

    @Override
    public CardInMachine getById(Integer id) {
        String cardInMachineName = "card_in_machine_" + id;
        CardInMachine redisCarInMachine = (CardInMachine) redisTemplate.opsForValue().get(cardInMachineName);
        if (redisCarInMachine != null)
            return redisCarInMachine;
        CardInMachine cardInMachine = cardInMachineMapper.selectById(id);
        redisTemplate.opsForValue().set(cardInMachineName, cardInMachine, 30, TimeUnit.SECONDS);
        return cardInMachine;
    }

    @Override
    public PageInfo<CardInMachine> getAll() {
        String pageName = "card_in_machine_all";
        PageHelper.startPage(1, pageSize);
        List<CardInMachine> redisCardInMachineList = (List<CardInMachine>) redisTemplate.opsForValue().get(pageName);
        if (redisCardInMachineList != null)
            return new PageInfo<>(redisCardInMachineList);
        List<CardInMachine> cardInMachineList = cardInMachineMapper.select(new CardInMachine());
        redisTemplate.opsForValue().set(pageName, cardInMachineList, 30, TimeUnit.SECONDS);
        return new PageInfo<>(cardInMachineList);
    }

    @Override
    public PageInfo<CardInMachine> getBy(PageInfo<CardInMachine> pageInfo) {
        //根据传入的页数与每页大小，组成页名
        String pageName = "card_in_machine_list_" + pageInfo.getPageNum() + "_" + pageInfo.getPageSize();
        //从redis中查询
        PageHelper.startPage(pageInfo.getPageNum(), pageSize);
        List<CardInMachine> redisCardInMachineList = (List<CardInMachine>) redisTemplate.opsForValue().get(pageName);
        if (redisCardInMachineList != null)
            return new PageInfo<>(redisCardInMachineList);
        //从数据库中查询
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<CardInMachine> cardInMachineList = cardInMachineMapper.select(pageInfo.getList().get(0));
        //放入数据库
        redisTemplate.opsForValue().set(pageName, cardInMachineList, 30, TimeUnit.SECONDS);
        return new PageInfo<>(cardInMachineList);
    }
}
